//ARDUINO 1.0+ ONLY
//Code Adapted from http://bildr.org/2011/06/arduino-ethernet-pin-control/


//*********************************************************************************************
//This sketch is for an Ethernet shield on a MEGA 2560.
//Standard ethernet shields need some software and hardware modifications to work with a MEGA.
//See this link for reference - http://mcukits.com/2009/04/06/arduino-ethernet-shield-mega-hack/
//Default SPI.h library has been modified for this sketch.
//On the Mega, pin 53 which is the hardware SS pin, must be assigned as an output otherwise the SPI interface will not work.
//*********************************************************************************************

#include <Ethernet.h>
#include <SPI.h>
boolean reading = false;

////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
  //byte ip[] = { 192, 168, 1, 66 };   //Manual setup only
  //byte gateway[] = { 192, 168, 0, 1 }; //Manual setup only
  //byte subnet[] = { 255, 255, 255, 0 }; //Manual setup only

  // if need to change the MAC address (Very Rare)
  byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

  EthernetServer server = EthernetServer(80); //port 80
////////////////////////////////////////////////////////////////////////

void setup(){
  Serial.begin(9600);

  //Pins 10,11,12 & 13 are used by the ethernet shield
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);

  //Ethernet.begin(mac);
  Ethernet.begin(mac);
  //Ethernet.begin(mac, ip, gateway, subnet); //for manual setup

  server.begin();
  Serial.println(Ethernet.localIP());
}

void loop(){
  // listen for incoming clients, and process request.
  checkForClient();
}

void checkForClient(){

  EthernetClient client = server.available();

  if (client) {

    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    boolean sentHeader = false;

    while (client.connected()) {
      if (client.available()) {

        if(!sentHeader){
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          sentHeader = true;
        }

        char c = client.read();

        if(reading && c == ' ') reading = false;
        if(c == '?') reading = true; //found the ?, begin reading the info

        if(reading){
          Serial.print(c);

           switch (c) {
            case '0':
              triggerPin(0, 1, client);
              break;
            case '1':
              triggerPin(1, 1, client);
              break;
            case '2':
              triggerPin(2, 1, client);
              break;
            case '3':
              triggerPin(3, 1, client);
              break;
            case '4':
              triggerPin(4, 1, client);
              break;
            case '5':
               triggerPin(5, 1, client);
              break;
            case '6':
               triggerPin(6, 1, client);
              break;
            case '7':
              triggerPin(7, 1, client);
              break;
            case '8':
              triggerPin(8, 1, client);
              break;
            case '9':
              triggerPin(9, 1, client);
              break;
            case 'a':
              triggerPin(0, 0, client);
              break;
            case 'b':
              triggerPin(1, 0, client);
              break;
            case 'c':
              triggerPin(2, 0, client);
              break;
            case 'd':
              triggerPin(3, 0, client);
              break;
            case 'e':
              triggerPin(4, 0, client);
              break;
            case 'f':
              triggerPin(5, 0, client);
              break;
            case 'g':
              triggerPin(6, 0, client);
              break;
            case 'h':
              triggerPin(7, 0, client);
              break;
            case 'i':
              triggerPin(8, 0, client);
              break;
            case 'j':
              triggerPin(9, 0, client);
              break;
          }
        }

        if (c == '\n' && currentLineIsBlank)  break;

        if (c == '\n') {
          currentLineIsBlank = true;
        }else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }

    delay(1); // give the web browser time to receive the data
    client.stop(); // close the connection:
  } 
}

void triggerPin(int pin, int command, EthernetClient client){
 //blink a pin - Client needed just for HTML output purposes.  
  client.print("Turning on pin ");
  client.println(pin);
  client.print("<br>");

  if (command == 1) {
    digitalWrite(pin, HIGH);
  }
  else {
    digitalWrite(pin, LOW);
  }
  
}
